package cn.edu.zut.soft.student.controller.admin.class_;

import cn.edu.zut.soft.student.bean.ResultBean;
import cn.edu.zut.soft.student.model.Class;
import cn.edu.zut.soft.student.model.ClassExample;
import cn.edu.zut.soft.student.model.Student;
import cn.edu.zut.soft.student.service.class_.ClassBo;
import cn.edu.zut.soft.student.service.class_.vo.ClassQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreatDate 2021/02/22 16:57
 */

@RestController
@RequestMapping("/admin/api/class")
public class ClassController {
    @Autowired
    private ClassBo classBo;

    @PostMapping("")
    public ResultBean<Integer> addClass(Class c) {
        return new ResultBean<>(classBo.addClass(c));
    }

    @DeleteMapping("/{classid}")
    public ResultBean<Integer> deleteClass(@PathVariable("classid") String classid) {
        return new ResultBean<>(classBo.deleteClass(classid));
    }

    @DeleteMapping("/list")
    public ResultBean<Integer> deleteClasses(String allClassIds) {
        String[] ids = allClassIds.substring(1, allClassIds.length() - 1).split(",");
        for (String id : ids) {
            classBo.deleteClass(id);
        }
        return new ResultBean<>();
    }

    @PutMapping("")
    public ResultBean<Integer> updateClass(Class c) {
        return new ResultBean<>(classBo.updateClass(c));
    }

    @GetMapping("/{classid}")
    public ResultBean<Class> queryClass(@PathVariable("classid") String classid) {
        return new ResultBean<>(classBo.queryClassById(classid));
    }

    @GetMapping("")
    public ResultBean<List<Class>> queryClasses(ClassQuery classQuery) {
        return new ResultBean<>(classBo.queryClasses(classQuery));
    }

    @GetMapping("/students/{classid}")
    public ResultBean<List<Student>> queryClassStudents(@PathVariable("classid") String classid) {
        return new ResultBean<>(classBo.queryClassStudents(classid));
    }
}
